<template>
<div>
  <el-row>
    <el-col :span="2" class="height40">
      所属站点:
    </el-col>
    <el-col :span="10">
      <el-input v-model="truckForm.nowSite" disabled></el-input>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="2" class="height40">
      司机:
    </el-col>
    <el-col :span="10">
      <el-select v-model="truckForm.empId" clearable placeholder="可不选">
        <el-option
          v-for="driver in drivers"
          :key="driver.empId"
          :label="driver.empName"
          :value="driver.empId">
        </el-option>
      </el-select>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="2" class="height40">
      类型:
    </el-col>
    <el-col :span="10">
      <el-radio v-model="truckForm.truckType" label="短途货车" border>短途货车</el-radio>
      <el-radio v-model="truckForm.truckType" label="长途货车" border>长途货车</el-radio>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="2" class="height40">
      车牌号码:
    </el-col>
    <el-col :span="10">
      <el-input v-model="truckForm.truckNumber" placeholder="必填"></el-input>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="5" :offset="5">
      <el-button type="primary" round icon="el-icon-success" @click="insertTruck">添加车辆</el-button><br>
    </el-col>
  </el-row>
</div>
</template>

<script>
import {selEmp} from 'network/employee'
import {addTruck} from 'network/truck'

let loginAccount = JSON.parse(sessionStorage.loginAccount)

export default {
  data() {
    return {
      drivers: [],
      truckForm: {
        siteId: loginAccount.siteId,
        empId: null, //司机,可不选
        truckType: '短途货车',  //长途货车,短途货车
        truckNumber: '',  //车牌号
        truckState: '空闲', //状态,有司机为待命,否则空闲
        stateInfo: '',  //状态信息,没啥软用
        nowSite: loginAccount.sitetb.siteName //当前所在网点,默认登录账号网点
      }
    }
  },
  methods: {
    insertTruck() {
      if(this.truckForm.truckNumber == null || this.truckForm.truckNumber == '') {
        this.$message({
          showClose: true,
          message: '请输入车牌号',
          type: 'error'
        });
        return false
      }
      if(this.truckForm.empId != null && this.truckForm.empId != '') {
        this.truckForm.truckState = "待命"
      }
      addTruck(this.truckForm).then(res => {
        if(res == "error") {
          this.$message.error("添加失败,请稍后重试");
        }else{
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          });
          this.truckForm.empId = null
          this.truckForm.truckNumber = null
        }
      }).catch(err => {
        console.log(err)
        this.$message.error("添加失败,请稍后重试");
      })
    }
  },
  mounted() {
    selEmp({siteId: loginAccount.siteId , postId: 7}).then(res => {
      for(let emp of res) {
        if(emp.truckId == 0 || emp.truckId == null) {
          this.drivers.push(emp)
        }
      }
    }).catch(err => {
      console.log(err)
    })
  }
}
</script>

<style scoped>
.height40{
  line-height:40px
}
.el-row{
  margin-bottom: 50px;
}
</style>
